package com.sunwayhorizo.health.ets.base.service; 

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.dto.ResultsDTO;
import com.sunwayhorizo.health.ets.base.common.BaseDao;
import com.sunwayhorizo.health.ets.base.persistent.EtModels;
import com.sunwayhorizo.health.ets.dto.EtModelsDTO;

/**
 * @author robo
 *
 */
@Name("EtModelsManager")
@Stateless
public class EtModelsManager extends BaseDao implements EtModelsManagerLocal {

	public ResultsDTO findEtModelsList(int currentPage,int pageSize,String modelsDesc)throws Exception{
		ResultsDTO  model = new ResultsDTO(currentPage,pageSize);
		String jpql = "select o from EtModels o where 1=1 order by o.clientCodeBase asc";
		Query query = getJpql(modelsDesc,jpql);
		if(currentPage!=0&&pageSize!=0)
		{
			query.setFirstResult(model.getBeginIndex());
			query.setMaxResults(model.getPageSize());
		}
		List<EtModelsDTO> returnList = new ArrayList<EtModelsDTO>();
		List<EtModels> list = query.getResultList();
		if(list != null && list.size() > 0 ){
			for(EtModels em:list){
				EtModelsDTO emdto = convertEtModelsDTO(em);
				returnList.add(emdto);
			}
		}
		model.setObject(returnList);
		if(currentPage!=0&&pageSize!=0)
		{
			model.setTotalRecords(findEtModelsCount(modelsDesc));
		}
		model.setResultFlag(true);
		model.setResultDesc("查询成功");
		return model;
	}

	public EtModelsDTO convertEtModelsDTO(EtModels em) {
		EtModelsDTO emdto = new EtModelsDTO();
		emdto.setModelDesc(em.getModelDesc());
		emdto.setModelName(em.getModelName());
		emdto.setCacheCodeBase(em.getCacheCodeBase());
		emdto.setClientCodeBase(em.getClientCodeBase());
		emdto.setCreatedBy(em.getCreatedBy());
		emdto.setCreationDate(em.getCreationDate());
		emdto.setImporterCodeBase(em.getImporterCodeBase());
		emdto.setLastUpdateDate(em.getLastUpdateDate());
		emdto.setLastUpdatedBy(em.getLastUpdatedBy());
		emdto.setLastUpdateLogin(em.getLastUpdateLogin());
		emdto.setLoaderCodeBase(em.getLoaderCodeBase());
		emdto.setServerCodeBase(em.getServerCodeBase());
		return emdto;
	}
	
	private Query getJpql(String modelsDesc,String jpql) {
		if(modelsDesc != null && !modelsDesc.equals("")){
			jpql += " and  o.modelDesc  like '%"+modelsDesc+"%' ";
		}
		Query query = entityManager2.createQuery(jpql);
		return query;
	}
	
	public int findEtModelsCount(String modelsDesc)throws Exception{
		String jpql = "select count(o.modelName) from  EtModels o where 1=1 ";
		Query query = getJpql(modelsDesc,jpql);
		List<Long> list = query.getResultList();
		if(list != null && list.size() >  0 ){
			return list.get(0).intValue();
		}
		return 0;
	}
	
	public ResultsDTO newEtModels(EtModelsDTO dto)throws Exception{
		ResultsDTO model = new ResultsDTO();
		EtModels ecs = new EtModels();
		ecs = covertEtModelsByDTO(dto, ecs);
		entityManager2.persist(ecs);
		model.setResultDesc("成功");
		model.setObject(ecs.getModelName());
		model.setResultFlag(true);
		return model;
	}

	private EtModels covertEtModelsByDTO(EtModelsDTO dto, EtModels ecs) {
		ecs.setModelName(dto.getModelName());
		ecs.setModelDesc(dto.getModelDesc());
		ecs.setCacheCodeBase(dto.getCacheCodeBase());
		ecs.setClientCodeBase(dto.getClientCodeBase());
		ecs.setCreatedBy(dto.getCreatedBy());
		ecs.setImporterCodeBase(dto.getImporterCodeBase());
		ecs.setLoaderCodeBase(dto.getLoaderCodeBase());
		ecs.setServerCodeBase(dto.getServerCodeBase());
		ecs.setCreatedBy(1l);
		ecs.setCreationDate(new Timestamp(new Date().getTime()));
		ecs.setLastUpdateDate(new Timestamp(new Date().getTime()));
		ecs.setLastUpdatedBy(1l);
		ecs.setLastUpdateLogin(1l);
		return ecs;
	}
	
	public ResultsDTO updateEtModels(EtModelsDTO dto)throws Exception{
		ResultsDTO model = new ResultsDTO();
		EtModels  ecs = entityManager2.find(EtModels.class, dto.getModelName());
		ecs = covertEtModelsByDTO(dto, ecs);
		entityManager2.merge(ecs);
		model.setResultDesc("成功");
		model.setResultFlag(true);
		model.setObject(ecs.getModelName());
		return model;
	}
	
	public ResultsDTO delEtModels(List  list)throws Exception{
		ResultsDTO resultsDTO = new ResultsDTO();
		if(list != null && list.size() > 0 ) {
			for(int i = 0 ; i < list.size() ; i++ ){
				String id = (String)list.get(i);
				EtModels ecs = entityManager2.find(EtModels.class, id);
				entityManager2.remove(ecs);
			}
		}
		resultsDTO.setResultDesc("删除成功");
		resultsDTO.setResultFlag(true);
		return resultsDTO;
	}
}
 